<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-button type="primary" @click="dialogOneVisible = true">添加角色</el-button>
      </el-row>
      <!-- 表单 -->
      <template>
        <el-table :data="list" border style="width: 100%">
          <el-table-column type="index" width="65px"></el-table-column>
          <el-table-column prop="roleName" label="角色名称" width="500px"></el-table-column>
          <el-table-column prop="roleDesc" label="角色描述" width="500px"></el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" @click=" editUserList(scope.row.id)">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete" @click="delUserList(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!-- 添加用户提示框 -->
      <el-dialog
        title="添加角色"
        :visible.sync="dialogOneVisible"
        width="960"
        :before-close="handleClose"
        @close="addClose"
      >
        <span>
          <!-- 角色名称  -->
          <el-form :model="addUser" label-width="100" ref="addUserRef">
            <el-form-item
              prop="roleName"
              label="角色名称"
              :rules="[{ required: true, message: '请输入角色名称', trigger: 'blur' }]"
            >
              <el-input v-model="addUser.roleName"></el-input>
            </el-form-item>
            <!-- 角色描述   -->
            <el-form-item
              prop="roleDesc"
              label="角色描述"
              :rules="[{ required: true, message: '请输入角色描述', trigger: 'blur' }]"
            >
              <el-input v-model="addUser.roleDesc"></el-input>
            </el-form-item>
          </el-form>
        </span>
        <!-- 底部按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogOneVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUserList">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 修改用户提示框 -->
      <el-dialog
        title="修改角色"
        :visible.sync="dialogTwoVisible"
        width="960"
        :before-close="handleClose"
        @close="addClose"
      >
        <span>
          <!-- 角色名称  -->
          <el-form :model="editUser" label-width="100" ref="addUserRef">
            <el-form-item
              prop="roleName"
              label="角色名称"
              :rules="[{ required: true, message: '请输入角色名称', trigger: 'blur' }]"
            >
              <el-input v-model="editUser.roleName"></el-input>
            </el-form-item>
            <!-- 角色描述   -->
            <el-form-item
              prop="roleDesc"
              label="角色描述"
              :rules="[{ required: true, message: '请输入角色描述', trigger: 'blur' }]"
            >
              <el-input v-model="editUser.roleDesc"></el-input>
            </el-form-item>
          </el-form>
        </span>
        <!-- 底部按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogTwoVisible= false">取 消</el-button>
          <el-button type="primary" @click="editSubmit(editUser)">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      dialogOneVisible: false,
      dialogTwoVisible: false,
      addUser: {
        roleName: '',
        roleDesc: ''
      },
      editUser: {
        
      }
    }
  },
  created() {
    this.userList()
  },
  methods: {
    //调用用户信息
    async userList() {
      const res = await this.$http('roles')
      // console.log(res.data.data)
      this.list = res.data.data
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    //添加用户对话框关闭事件
    addClose() {
      this.$refs.addUserRef.resetFields()
    },
    //添加用户
    async addUserList() {
      //非空校验
      if (
        this.addUser.roleName.trim().length === 0 ||
        this.addUser.roleDesc.trim().length === 0
      )
        return this.$message.warning('请输入添加信息')
      const res = await this.$http.post('roles', this.addUser)
      // console.log(res)
      if (res.status !== 200) {
        this.$message.error('添加用户失败')
      } else {
        this.$message.success('添加用户成功')
      }
      this.dialogOneVisible = false
      //更新用户信息
      this.userList()
    },
    // 修改角色信息
    async editUserList(id) {
      this.dialogTwoVisible = true

      const res = await this.$http('roles/' + id)
      console.log(res.data.data)
      this.editUser = res.data.data
      //   if (res.data.meta.status !== 200) {
      //     this.$message.error('修改用户失败')
      //   } else {
      //     this.$message.success('修改用户成功')
      //   }
    },
    //提交修改信息
    async editSubmit() {
      if (this.editUser.roleName.trim().length === 0)
        return this.$message.warning('请输入修改内容')
      console.log(this.editUser.roleName)
      const res = await this.$http.put('roles/' +this.editUser.roleId, {
        roleName: this.editUser.roleName,
        roleDesc: this.editUser.roleDesc
      })
      console.log(res)
      if (res.status !== 200) {
        this.$message.error('修改用户失败')
      } else {
        this.$message.success('修改用户成功')
      }
      this.dialogTwoVisible = false
      this.userList()
    },
    //删除
    async delUserList(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          await this.$http.delete('roles/' + id)
          this.userList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
